package com.hht.touchlibs

import android.content.Context
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import com.hht.touchlibs.BaseTouchListener
import com.hht.touchlibs.UserTouchEvent


/**
 * Author:chen'hu
 * Time:2024/6/13 13:41
 * Description:This is UserTouchListener
 */
abstract class UserTouchListener(mContext: Context, mCurView: View, mTouchEvent: UserTouchEvent) :
    BaseTouchListener {
    protected var mView: View
    protected var mGestureDetector: GestureDetector
    protected var mDownX = 0f
    protected var mDownY = 0f
    protected var mCurrentX = 0
    protected var mCurrentY = 0
    protected var mUserTouchEvent: UserTouchEvent

    init {
        mView = mCurView
        mView.setOnTouchListener(this)
        mUserTouchEvent = mTouchEvent
        mGestureDetector = GestureDetector(mContext, this)

    }

    override fun onDown(e: MotionEvent): Boolean {
        mDownX = e.x
        mDownY = e.y
        return true
    }

    override fun onShowPress(e: MotionEvent?) {}

    override fun onLongPress(e: MotionEvent?) {}

    override fun onSingleTapUp(e: MotionEvent?): Boolean {
        mUserTouchEvent.onTouchClick(mView)
        return false
    }

    override fun onFling(
        e1: MotionEvent?,
        e2: MotionEvent?,
        velocityX: Float,
        velocityY: Float,
    ): Boolean {
        return true
    }

    override fun onTouch(v: View?, event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> mView.isPressed = v !is ViewGroup
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                mView.isPressed = false
                mUserTouchEvent.onTouchCancel(mView)
            }
        }
        return mGestureDetector.onTouchEvent(event)
    }

    override fun onRecoveryTouch() {
    }
}